// pages/ConsumptionCoupon/index.js
const common = require('../../../../public.js')
const utils = require('../../../../utils/utils.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    coupFlag:false,  //控制更多
    show: true,
    managerId:'',
    isLoadingBusy: false,
    cityId: 0,
    regionId: '',
    provinceId: '',
    discountCouponList: [
      // {exceedMoney:500,surplusNums:3,discountsMoney:1000,
      //   couponName:'文旅嘉年华济南通用券',
      //   effectiveTime:48},{couponName:'文旅嘉年华济南通用券',effectiveTime:48}
      ],
    adminDiscountCouponList: [
  //       {
  //         exceedMoney:500,
  //         surplusNums:3,
  //         discountsMoney:1000,
  //         name:'文旅嘉年华济南通用券',
  //         effectiveTime:48,
  //         type:4
  //     },
  //     {
  //       exceedMoney:500,
  //       surplusNums:3,
  //       discountsMoney:1000,
  //       name:'文旅嘉年华济南通用券',
  //       effectiveTime:48,
  //       type:12
  //   },
  //   {
  //     exceedMoney:500,
  //     surplusNums:3,
  //     discountsMoney:1000,
  //     name:'文旅嘉年华济南通用券',
  //     effectiveTime:48,
  //     type:2
  // },
      
    ], 
    isCityPage: true,
    from: null,
    imageId: 0,
    active: 0,
    pageNum: 0,
    pageNums:0,
    desc:"",
    benefitListData:[
      // {
      //   address: "浙江大学山东工业技术研究院",
      //   adminCouponNums: 0,
      //   articleNums: 0,
      //   name: "春天里",
      //   region: "薛城区",
      //   storeImage: "http://feiyangimage.oss-cn-shanghai.aliyuncs.com/shop/1614669475605526.png",
      //   storeTypeName: "景区景点",
      // }
    ],
    nomore:true,
    isRealname:0,
    title:"",
  },

  toClose(){
    this.setData({ show: false });
  },
  onClickShow() {
    this.setData({ show: true });
  },

  onClickHide() {
    this.setData({ coupFlag: false });
  },

  noop() {},
  /**
   * 生命周期函数--监听页面加载
   */
  goBack() {
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; //上一个页面
    // 从朋友圈进入的时候，页面栈为空，这里需要判断是否有上个页面
    if (prevPage) {
        wx.navigateBack({
            delta: 1
        })
    } else {
        wx.redirectTo({
            url: '/pages/index/index',
        })
    }
},
  onChange(event) {
    this.setData({
      active:event.detail.name,
      pageNum:0
    },()=>{
      if(this.data.active==0){  //0是政府券
        console.log("政府券")
        this.getCityCoupons(true)  //政府券
     }else if(this.data.active==2) {    //2是企业券
       console.log("1211")
       this.getCityList(true)
     }
    })

  },
  // 公告
  notice(){
    var that = this
    common.post('/wlplatfrom/store/xcx/announcement', {}, function (res) {
      if (res.status === 200) {
         console.log(res.data,"11111")
          that.setData({
            title:res.data.data.title,
            desc:res.data.data.description
          })
        } else {
          
    } 
  })
},
  // 公告
//   notice_two(){
//     var that = this
//     common.post('/wlplatfrom/store/xcx/announcement', {}, function (res) {
//       if (res.status === 200) {
//          console.log(res.data,"11111")
        
//         } else {
          
//     } 
//   })
// },
  onLoad: function (options) {
    var that = this
    // 处理实名认证返回没有option的问题
    console.log("4444",options)
    console.log(this.data.active,"onLoad")
    let capsule = wx.getMenuButtonBoundingClientRect()
    this.setData({
        height: capsule.height,
        top: capsule.top,
        storeId: options.id,
        managerId:options.managerId ? options.managerId : '' ,
       isOffLine:options.isOffLine ? options.isOffLine : ''
    },()=>{
      wx.getStorage({
        key: 'user',
        success: function (res) {
          that.setData({
            userInfo: res.data.user,
            token: res.data.token,
            realname: res.data.user.isRealname == 1
          },()=>{
            console.log(that.data.realname)
          })
        },
        fail: function () {
          that.setData({
            userInfo: null
          })
        }
      })
      if(this.data.active==2){
        this.getCityList(true)
        return
      }
  
      this.getCityCoupons(true)  //政府券
      this.notice()
      this.moreCoups(true)
    })
    // var managerId = options.managerId ? options.managerId : 123
    // that.setData({
      
    // })
  },
  // 去实名
  toRealName() {
    if (this.data.userInfo === null) {
      // 未登录
      common.showLoginPage()
    } else if (this.data.userInfo.isRealname != 1) {
      common.toRealName()
    } else {
      // common.toRealName()
      common.showToast('您已实名')
    }
  },
  onShow: function () {
    const that = this
   
    // this.notice_two()
  },
  getCouponFinish(e) {
    console.log(e,"ce")
    console.log("成功")
    console.log(this.data.type)
   if(e.detail.type==2){
     //惠民券
     this.setData({
      coupFlag:true
    })
    this.moreCoups(true)
   }
    this.getCityCoupons()
  },

  loadMore(){
    if(!this.data.active){  //0是政府券
       console.log("政府券")
      //  this.getCityCoupons(false)  //政府券
    }else {    //2是企业券
      console.log("1211")
      this.getCityList(false)
    }
  },
  // 跳转对应店铺
  toStore(e){
    common.gotoStore(e.currentTarget.dataset.item)
  },
  /**
   * 获取城市列表
   */
  getCityList(refresh = false){
    var that = this
    // if(!that.data.nomore)  return  //没有数据禁止请求
    this.setData({
      isLoadingBusy: true
    })
    let params=null
    const pageNum = refresh ? 1 : this.data.pageNum + 1
      params = {
          pageNum,
          pageSize:10,
          managerId: this.data.managerId
        }
      
    common.post('/cityDisCoupon/getCityDisCouponFreeStoreListByManagerId', params, function (res) {
      if (res.status === 200) {
        that.data.pageNum = pageNum
        if (!refresh) {
           if (res.data.length === 0) {
            that.setData({
              nomore:false  //无数据不请求
            })
          }
            that.setData({
                benefitListData: [...that.data.benefitListData, ...res.data]
            })
        } else {
            if (res.data.length === 0) {
          
            }
            that.setData({
                benefitListData: res.data,
            })
        }
       
      } else {
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        isLoadingBusy: false
      })
    })
      
  },

  /**
   * 获取优惠券列表
   */
  moreCoups(refresh = false) {
    var that = this
    // if(!that.data.nomore)  return  //没有数据禁止请求
    this.setData({
      isLoadingBusy: true
    })
    const pageNum = refresh ? 1 : this.data.pageNums + 1
    let url = '/cityDisCoupon/getMoreCouponListByManagerId' 
    let params ={
      pageNum,
      pageSize:10,
      managerId:that.data.managerId
    }
    common.post(url, params, function (res) {
      if (res.status === 200) {
        that.data.pageNums = pageNum
        
        //  if(res.data.discountCouponList.length<10 && res.data.discountCouponList.length<10){
        //   that.setData({
        //     nomore:false  //无数据不请求
        //   })
        //  }
          that.setData({
            coupList:res.data
          })
      } else {
        // that.setData({
        //   discountCouponList: [],
        //   adminDiscountCouponList: []
        // })
        common.showRequestErrMsg(res)
      }
    },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      })
  },
  getCityCoupons(refresh = false) {
    var that = this
    // if(!that.data.nomore)  return  //没有数据禁止请求
    this.setData({
      isLoadingBusy: true
    })
  
    const pageNum = refresh ? 1 : this.data.pageNum + 1
    let url = '/cityDisCoupon/getMoreCouponListByActivId' 
    let params ={
      // pageNum,
      // pageSize:10,
      // managerId:that.data.managerId
      activId:that.data.managerId,
      isOffLine:that.data.isOffLine,
    }
    common.post(url, params, function (res) {
      if (res.status === 200) {
        that.data.pageNum = pageNum
         if(res.data.length<10 ){
          that.setData({
            nomore:false  //无数据不请求
          })
         }
          // 0 线下 1线上
          if(that.data.isOffLine==0){
           
            that.setData({
              // discountCouponList: res.data.discountCouponList,
              adminDiscountCouponList: res.data
            })
          }else{
            that.setData({
              discountCouponList: res.data,
              // adminDiscountCouponList: res.data.adminDiscountCouponList
            })
          }
        
      } else {
        that.setData({
          discountCouponList: [],
          adminDiscountCouponList: []
        })
        common.showRequestErrMsg(res)
      }
    },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      })
  },

  toDetail(e) { // 跳转详情页
    let dcId = e.currentTarget.dataset.id
    console.log(e,"ceshicehsil领券")
    wx.navigateTo({
      url: '/pages/cultureCoupons/cultureCouponsDetail/index?dcId=' + dcId
    })
  },

  toPlatformCoupons(e) {
    // let dcId = e.currentTarget.dataset.id
    // wx.navigateTo({
    //   url: '/pages/cultureCoupons/platformCoupons/index?dcId=' + dcId
    // })
  },

  getTopicTitle() {
    return utils.getTopicTitle(!common.isEmptyString(this.data.provinceId) ? this.data.provinceId : this.data.cityId, this.data.regionId)
  },

  getJumpParma() {
    let tmpList = []
    !common.isEmptyString(this.data.provinceId) && tmpList.push('provinceId=' + this.data.provinceId)
    !common.isEmptyString(this.data.cityId) && tmpList.push('cityId=' + this.data.cityId)
    !common.isEmptyString(this.data.regionId) && tmpList.push('regionId=' + this.data.regionId)
    return tmpList.join('&')
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    return {
      title: '补贴来了，海量消费券，等你来抢！',
      imageUrl: 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/share.jpg',
      success: (res) => { },
      fail: (res) => {
        // 分享失败
      }
    }
  },
  onShareTimeline() {
    const query = 'from=timeline&' + this.getJumpParma()
    return {
      title:'补贴来了，海量消费券，等你来抢！',
      query
    }
  },

})